package org.rsbot.script.internal;

import java.applet.Applet;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.Random;
import org.rsbot.bot.Bot;
import org.rsbot.client.Client;
import org.rsbot.client.input.Canvas;
import org.rsbot.client.input.Keyboard;
import org.rsbot.client.input.Mouse;

public class InputManager
{
  private final Random random = new Random();
  private final MouseHandler mouseHandler = new MouseHandler(this);
  private final Bot bot;
  private byte dragLength = 0;

  private int side = random(1, 5);

  public InputManager(Bot paramBot) {
    this.bot = paramBot;
  }

  private boolean isOnCanvas(int paramInt1, int paramInt2) {
    return (paramInt1 > 0) && (paramInt1 < this.bot.getCanvas().getWidth()) && (paramInt2 > 0) && (paramInt2 < this.bot.getCanvas().getHeight());
  }

  public void clickMouse(boolean paramBoolean) {
    if (!getClient().getMouse().isPresent())
      return;
    pressMouse(getX(), getY(), paramBoolean);
    sleepNoException(random(50, 100));
    releaseMouse(getX(), getY(), paramBoolean);
  }

  public void dragMouse(int paramInt1, int paramInt2)
  {
    pressMouse(getX(), getY(), true);
    sleepNoException(random(300, 500));
    windMouse(getX(), getY(), paramInt1, paramInt2);
    sleepNoException(random(300, 500));
    releaseMouse(paramInt1, paramInt2, true);
  }

  private void gainFocus()
  {
    Canvas localCanvas = getCanvasWrapper();
    if (!localCanvas.hasFocus())
      localCanvas.setFocused(true);
  }

  private Canvas getCanvasWrapper()
  {
    return (Canvas)getTarget().getComponent(0);
  }

  private Client getClient() {
    return this.bot.getClient();
  }

  private char getKeyChar(char paramChar) {
    if ((paramChar >= '$') && (paramChar <= '(')) {
      return '\000';
    }
    return paramChar;
  }

  private Applet getTarget() {
    return (Applet)getClient();
  }

  public int getX() {
    return getClient().getMouse().getX();
  }

  public int getY() {
    return getClient().getMouse().getY();
  }

  public void holdKey(int paramInt1, int paramInt2)
  {
    KeyEvent localKeyEvent = new KeyEvent(getTarget(), 401, System.currentTimeMillis(), 0, paramInt1, (char)paramInt1);
    getClient().getKeyboard()._keyPressed(localKeyEvent);

    if (paramInt2 > 500) {
      localKeyEvent = new KeyEvent(getTarget(), 401, System.currentTimeMillis() + 500L, 0, paramInt1, (char)paramInt1);
      getClient().getKeyboard()._keyPressed(localKeyEvent);
      i = paramInt2 - 500;
      for (int j = 37; j < i; j += random(20, 40)) {
        localKeyEvent = new KeyEvent(getTarget(), 401, System.currentTimeMillis() + j + 500L, 0, paramInt1, (char)paramInt1);
        getClient().getKeyboard()._keyPressed(localKeyEvent);
      }
    }
    int i = paramInt2 + random(-30, 30);
    localKeyEvent = new KeyEvent(getTarget(), 402, System.currentTimeMillis() + i, 0, paramInt1, (char)paramInt1);
    getClient().getKeyboard()._keyReleased(localKeyEvent);
  }

  public void hopMouse(int paramInt1, int paramInt2) {
    moveMouse(paramInt1, paramInt2);
  }

  private void loseFocus()
  {
    Canvas localCanvas = getCanvasWrapper();
    if (localCanvas.hasFocus())
      localCanvas.setFocused(false);
  }

  private void moveMouse(int paramInt1, int paramInt2)
  {
    MouseEvent localMouseEvent;
    if (getClient().getMouse().isPressed()) {
      localMouseEvent = new MouseEvent(getTarget(), 506, System.currentTimeMillis(), 0, paramInt1, paramInt2, 0, false);
      getClient().getMouse().sendEvent(localMouseEvent);
      if ((this.dragLength & 0xFF) != 255) {
        this.dragLength = (byte)(this.dragLength + 1);
      }
    }

    if (!getClient().getMouse().isPresent()) {
      if (isOnCanvas(paramInt1, paramInt2)) {
        localMouseEvent = new MouseEvent(getTarget(), 504, System.currentTimeMillis(), 0, paramInt1, paramInt2, 0, false);
        getClient().getMouse().sendEvent(localMouseEvent);
      }
    } else if (!isOnCanvas(paramInt1, paramInt2)) {
      localMouseEvent = new MouseEvent(getTarget(), 505, System.currentTimeMillis(), 0, paramInt1, paramInt2, 0, false);
      getClient().getMouse().sendEvent(localMouseEvent);
      int i = this.bot.getCanvas().getWidth(); int j = this.bot.getCanvas().getHeight(); int k = 50;
      if (paramInt1 < k) {
        if (paramInt2 < k)
          this.side = 4;
        else if (paramInt2 > j + k)
          this.side = 2;
        else
          this.side = 1;
      }
      else if (paramInt1 > i)
        this.side = 3;
      else
        this.side = random(1, 5);
    }
    else if (!getClient().getMouse().isPressed()) {
      localMouseEvent = new MouseEvent(getTarget(), 503, System.currentTimeMillis(), 0, paramInt1, paramInt2, 0, false);
      getClient().getMouse().sendEvent(localMouseEvent);
    }
  }

  public void moveMouse(int paramInt1, int paramInt2, int paramInt3, int paramInt4)
  {
    moveMouse(10, paramInt1, paramInt2, paramInt3, paramInt4);
  }

  public void moveMouse(int paramInt1, int paramInt2, int paramInt3, int paramInt4, int paramInt5)
  {
    int i = getX(); int j = getY();
    if (!isOnCanvas(i, j))
    {
      switch (this.side) {
      case 1:
        i = -1;
        j = random(0, this.bot.getCanvas().getHeight());
        break;
      case 2:
        i = random(0, this.bot.getCanvas().getWidth());
        j = this.bot.getCanvas().getHeight() + 1;
        break;
      case 3:
        i = this.bot.getCanvas().getWidth() + 1;
        j = random(0, this.bot.getCanvas().getHeight());
        break;
      case 4:
        i = random(0, this.bot.getCanvas().getWidth());
        j = -1;
      }
    }

    windMouse(paramInt1, i, j, random(paramInt2, paramInt2 + paramInt4), random(paramInt3, paramInt3 + paramInt5));
  }

  public void pressKey(char paramChar)
  {
    KeyEvent localKeyEvent = new KeyEvent(getTarget(), 401, System.currentTimeMillis(), 0, paramChar, getKeyChar(paramChar));
    getClient().getKeyboard()._keyPressed(localKeyEvent);
  }

  private void pressMouse(int paramInt1, int paramInt2, boolean paramBoolean) {
    if ((getClient().getMouse().isPressed()) || (!getClient().getMouse().isPresent()))
      return;
    MouseEvent localMouseEvent = new MouseEvent(getTarget(), 501, System.currentTimeMillis(), 0, paramInt1, paramInt2, 1, false, paramBoolean ? 1 : 3);
    getClient().getMouse().sendEvent(localMouseEvent);
  }

  public int random(int paramInt1, int paramInt2) {
    int i = Math.abs(paramInt2 - paramInt1);
    return Math.min(paramInt1, paramInt2) + (i == 0 ? 0 : this.random.nextInt(i));
  }

  public void releaseKey(char paramChar)
  {
    KeyEvent localKeyEvent = new KeyEvent(getTarget(), 402, System.currentTimeMillis(), 512, paramChar, getKeyChar(paramChar));
    getClient().getKeyboard()._keyReleased(localKeyEvent);
  }

  private void releaseMouse(int paramInt1, int paramInt2, boolean paramBoolean) {
    if (!getClient().getMouse().isPressed())
      return;
    MouseEvent localMouseEvent = new MouseEvent(getTarget(), 502, System.currentTimeMillis(), 0, paramInt1, paramInt2, 1, false, paramBoolean ? 1 : 3);
    getClient().getMouse().sendEvent(localMouseEvent);

    if ((this.dragLength & 0xFF) <= 3) {
      localMouseEvent = new MouseEvent(getTarget(), 500, System.currentTimeMillis(), 0, paramInt1, paramInt2, 1, false, paramBoolean ? 1 : 3);
      getClient().getMouse().sendEvent(localMouseEvent);
    }

    this.dragLength = 0;
  }

  public void sendKey(char paramChar) {
    sendKey(paramChar, 0);
  }

  private void sendKey(char paramChar, int paramInt) {
    int i = 0;
    int j = paramChar;
    if ((paramChar >= 'a') && (paramChar <= 'z'))
      j -= 32;
    else if ((paramChar >= 'A') && (paramChar <= 'Z'))
      i = 1;
    KeyEvent localKeyEvent;
    int k;
    if ((j == 37) || (j == 38) || (j == 40)) {
      localKeyEvent = new KeyEvent(getTarget(), 401, System.currentTimeMillis() + paramInt, 0, j, getKeyChar(paramChar), 1);
      getClient().getKeyboard()._keyPressed(localKeyEvent);
      k = random(50, 120) + random(0, 100);
      localKeyEvent = new KeyEvent(getTarget(), 402, System.currentTimeMillis() + k, 0, j, getKeyChar(paramChar), 1);
      getClient().getKeyboard()._keyReleased(localKeyEvent);
    }
    else if (i == 0) {
      localKeyEvent = new KeyEvent(getTarget(), 401, System.currentTimeMillis() + paramInt, 0, j, getKeyChar(paramChar), 1);
      getClient().getKeyboard()._keyPressed(localKeyEvent);

      localKeyEvent = new KeyEvent(getTarget(), 400, System.currentTimeMillis() + 0L, 0, 0, paramChar, 0);
      getClient().getKeyboard()._keyTyped(localKeyEvent);

      k = random(50, 120) + random(0, 100);
      localKeyEvent = new KeyEvent(getTarget(), 402, System.currentTimeMillis() + k, 0, j, getKeyChar(paramChar), 1);
      getClient().getKeyboard()._keyReleased(localKeyEvent);
    }
    else {
      k = random(25, 60) + random(0, 50);
      localKeyEvent = new KeyEvent(getTarget(), 401, System.currentTimeMillis() + k, 64, 16, '\000', 2);
      getClient().getKeyboard()._keyPressed(localKeyEvent);

      localKeyEvent = new KeyEvent(getTarget(), 401, System.currentTimeMillis() + paramInt, 64, j, getKeyChar(paramChar), 1);
      getClient().getKeyboard()._keyPressed(localKeyEvent);

      localKeyEvent = new KeyEvent(getTarget(), 400, System.currentTimeMillis() + 0L, 64, 0, paramChar, 0);
      getClient().getKeyboard()._keyTyped(localKeyEvent);

      int m = random(50, 120) + random(0, 100);
      localKeyEvent = new KeyEvent(getTarget(), 402, System.currentTimeMillis() + m, 64, j, getKeyChar(paramChar), 1);
      getClient().getKeyboard()._keyReleased(localKeyEvent);

      int n = random(25, 60) + random(0, 50);
      localKeyEvent = new KeyEvent(getTarget(), 402, System.currentTimeMillis() + n, 64, 16, '\000', 2);
      getClient().getKeyboard()._keyReleased(localKeyEvent);
    }
  }

  public void sendKeys(String paramString, boolean paramBoolean)
  {
    sendKeys(paramString, paramBoolean, 100, 200);
  }

  public void sendKeys(String paramString, boolean paramBoolean, int paramInt) {
    sendKeys(paramString, paramBoolean, paramInt, paramInt);
  }

  public void sendKeys(String paramString, boolean paramBoolean, int paramInt1, int paramInt2) {
    char[] arrayOfChar1 = paramString.toCharArray();
    for (char c : arrayOfChar1) {
      sendKey(c, random(paramInt1, paramInt2));
      sleepNoException(random(paramInt1, paramInt2));
    }
    if (paramBoolean)
      sendKey('\n', random(paramInt1, paramInt2));
  }

  public void sendKeysInstant(String paramString, boolean paramBoolean)
  {
    for (char c : paramString.toCharArray()) {
      sendKey(c, 0);
    }
    if (paramBoolean)
      sendKey('\n', 0);
  }

  public void sleepNoException(long paramLong)
  {
    try {
      Thread.sleep(paramLong);
    }
    catch (Exception localException)
    {
    }
  }

  public void windMouse(int paramInt1, int paramInt2, int paramInt3, int paramInt4)
  {
    windMouse(10, paramInt1, paramInt2, paramInt3, paramInt4);
  }

  public void windMouse(int paramInt1, int paramInt2, int paramInt3, int paramInt4, int paramInt5)
  {
    this.mouseHandler.moveMouse(paramInt1, paramInt2, paramInt3, paramInt4, paramInt5, 0, 0);
  }
}